package com.huang.Service.ServiceImpl;

import com.huang.Dao.ProductDao;
import com.huang.Dao.ProductDao02;
import com.huang.Pojo.Product;
import com.huang.Service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;


@Service
public class ProductServiceImpl implements ProductService {


    @Resource
    private ProductDao productDao;

    @Autowired
    private ProductDao02 productDao02;

    @Override
    public Product findByPid(Integer pid) {
        Product product = productDao.findById(pid).get();
        return product;
    }

    @Override
    @Transactional
    public void reduceInventory(Integer pid, Integer number) {
        Product product = productDao02.getProductById(pid);
        product.setStock(product.getStock()-number);

//        模拟异常
        int i = 1 / 0;
        productDao.save(product);
    }
}
